package com.silence.basic_learn;

import java.util.Random;
import java.util.Scanner;

public class Login_String_Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("验证码:"+createCode(4));
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入登陆名称");
            String loginName = sc.next();
            System.out.println("请输入登陆密码");
            String password = sc.next();

            if (login(loginName, password)) {
                System.out.println("登陆成功");
                break;
            } else {
                System.out.println("登陆失败，请重试");
                if (i == 2) {
                    System.out.println("登陆失败，3次尝试结束");
                } else {
                    System.out.println("登陆失败，您还有" + (2 - i) + "次机会");
                }
            }
        }
    }

    public static boolean login(String loginName, String password) {
        String okLoginName = "123";
        String okPassword = "123456";
        return okLoginName.equals(loginName) && okPassword.equals(password);
    }

    public static String createCode(int n) {
        String code = "";
        String data = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        Random r = new Random();
        for (int i = 0; i < n; i++) {
            int index = r.nextInt(data.length());
            code += data.charAt(index);
        }
        return code;
    }
}
